package edu.rutgers.rucampusinfo.directory;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.rutgers.rucampusinfo.PMF;
import edu.rutgers.rucampusinfo.tables.Role;
public class RolesList extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = -172420556006592977L;
	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
		RequestDispatcher rd = null;
		resp.setContentType("text/html");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String sort = req.getParameter("sort");
		Query query = null;
		if (sort == null)
			sort = "name";			
		try{
			//if ((sort.equals("name")) )
		//	{
			query = pm.newQuery("SELECT FROM "+Role.class.getName()+" order by role");
		//	}
		//	else if (sort.equals("size"))
		//	{
		//	query = pm.newQuery("SELECT FROM "+EmpDir.class.getName()+" order by roleid");
				
		//	}	
			List<Role> results = (List<Role>)query.execute();
			int size = results.size();
			req.setAttribute("size", size);
			req.setAttribute("roleslist", results);
			req.setAttribute("sort", "name");
		    rd = getServletContext().getRequestDispatcher("/employeedir.jsp");
		    rd.forward(req, resp);
		}catch(Exception Ex)
		{
		//		success = "f2";
		}
        finally {
        	pm.close();
        }
    }
}